package controller;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import view.GameView;
import view.GameWindow;

public class FullScreenController extends Controller {

	private GameWindow window;
	private boolean isFullScreen;
	
	public FullScreenController(GameWindow window) {
		
		this.window = window;
		this.isFullScreen = false;
	}
	
	@Override
	public List<GameControl> getKeyControls() {
		List<GameControl> gcs = new ArrayList<GameControl>();
		
		GameControl gc = new TimedGameControl(KeyEvent.VK_F11, "TOGGLE_FULL_SCREEN", "Toggle Full-Screen Mode", 2000) {

			@Override
			protected void keyReleased() {
				toggleFullScreen();
			}
			
			@Override
			protected void execute() {
				// Do nothing
			}
			
		};
		
		gcs.add(gc);
		
		return (gcs);
	}
	
	public void toggleFullScreen() {
		
		GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
		
		try {
			
			GameView gameView = window.getGameView();
			window.dispose();
			window = new GameWindow();
			window.setGameView(gameView);
			
			if (isFullScreen) {
				
				window.setUndecorated(false);
				device.setFullScreenWindow(null);
				isFullScreen = false;
			}
			else {
				
				window.setUndecorated(true);
				device.setFullScreenWindow(window);
				isFullScreen = true;
			}
			
			window.setVisible(true);
		}
		catch (Exception ex) {
			device.setFullScreenWindow(null);
			System.err.print(ex.getMessage());
		}
	}
		
	@Override
	public void reset() {
		// nothing
	}
}